package com.local.hwcard;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class ConfigActivity extends AppCompatActivity {
    private EditText etServerIp;
    private EditText etVolume;
    private EditText etRoom;
    private EditText etInputId;
    private Button btnConfirm;
    private CheckBox cbNotification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config);

        // 初始化控件
        etServerIp = findViewById(R.id.et_server_ip);
        etVolume = findViewById(R.id.et_volume);
        etRoom = findViewById(R.id.et_room);
        etInputId = findViewById(R.id.et_input_id);
        cbNotification = findViewById(R.id.cb_notification);
        btnConfirm = findViewById(R.id.btn_confirm);

        // 读取已保存的配置信息并显示
        loadConfig();

        // 确认按钮点击事件
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveConfig();
                setResult(RESULT_OK);
                finish();
            }
        });
    }

    // 保存配置信息到文件
    private void saveConfig() {
        String serverIp = etServerIp.getText().toString().trim();
        String volume = etVolume.getText().toString().trim();
        String room = etRoom.getText().toString().trim();
        String inputId = etInputId.getText().toString().trim();
        boolean hasNotification = cbNotification.isChecked();

        // 验证输入
        if (serverIp.isEmpty()) {
            Toast.makeText(this, "请输入服务器IP", Toast.LENGTH_SHORT).show();
            return;
        }

        if (volume.isEmpty()) {
            Toast.makeText(this, "请输入音量大小", Toast.LENGTH_SHORT).show();
            return;
        }

        if (inputId.length() > 3) {
            Toast.makeText(this, "名字不能超过3个字符", Toast.LENGTH_SHORT).show();
            return;
        }
        if (DeviceInfor.hasDigit(inputId)){
            Toast.makeText(this, "名字不能有数字", Toast.LENGTH_SHORT).show();
            return;
        }

        String check = "false";
        if (hasNotification) {
            check = "true";
        }

        DeviceInfor.saveConfig(serverIp, volume, room, inputId, check);
    }

    // 加载已保存的配置信息
    private void loadConfig() {
        // 设置到控件上
        etServerIp.setText(DeviceInfor.getServerIp());
        etVolume.setText(DeviceInfor.getVolume());
        etRoom.setText(DeviceInfor.getRoom());
        etInputId.setText(DeviceInfor.getInputID());

        boolean notification = false;
        if (DeviceInfor.getCheck().equals("true")) {
            notification = true;
        }
        cbNotification.setChecked(notification);
    }
}
